package com.chenfubing.ps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenfubing.ps.entity.Access;
import com.chenfubing.ps.entity.Community;
import com.chenfubing.ps.entity.pojo.CountPojo;
import com.chenfubing.ps.entity.pojo.WeekPojo;
import com.chenfubing.ps.mapper.AccessMapper;
import com.chenfubing.ps.mapper.AppointmentMapper;
import com.chenfubing.ps.mapper.CommunityMapper;
import com.chenfubing.ps.mapper.SpaceMapper;
import com.chenfubing.ps.service.IndexService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author chenfubing
 */
@Service
public class IndexServiceImpl implements IndexService {

    @Resource
    private AccessMapper accessMapper;

    @Resource
    private AppointmentMapper appointmentMapper;

    @Resource
    private SpaceMapper spaceMapper;

    @Resource
    private CommunityMapper communityMapper;

    @Override
    public CountPojo getCountPojo() {
        Integer access = accessMapper.selectCount(new QueryWrapper<Access>().between("create_time",  LocalDateTime.now().minusDays(7),LocalDateTime.now()));
        Integer appointment = appointmentMapper.selectCount(new QueryWrapper<>());
        Integer in = accessMapper.selectCount(new QueryWrapper<Access>().eq("way", 1));
        Integer out = accessMapper.selectCount(new QueryWrapper<Access>().eq("way", 0));
        int space = in - out;
        Community community = communityMapper.selectById(1);
        return new CountPojo().setAccessCount(access).setAppointmentCount(appointment).setSpaceCount(community.getCommunitySpaceNumber() - space);
    }

    @Override
    public WeekPojo getWeekPojo() {
        LocalDateTime date = LocalDateTime.now();
        WeekPojo weekPojo = new WeekPojo().setOne(accessMapper.selectCountOfAccess(date.minusDays(1),date ))
                .setTwo(accessMapper.selectCountOfAccess(date.minusDays(2),date.minusDays(1)))
                .setThree(accessMapper.selectCountOfAccess(date.minusDays(3),date.minusDays(2)))
                .setFour(accessMapper.selectCountOfAccess( date.minusDays(4),date.minusDays(3)))
                .setFive(accessMapper.selectCountOfAccess( date.minusDays(5), date.minusDays(4)))
                .setSix(accessMapper.selectCountOfAccess(date.minusDays(6), date.minusDays(5)))
                .setSeven(accessMapper.selectCountOfAccess( date.minusDays(7), date.minusDays(6)));
        System.out.println(weekPojo);
        return weekPojo;
    }


}
